var express = require('express');
const axios = require('axios');
const config = require('./config');
var router = express.Router();

/* GET home page. */
router.get('/', function(req, res, next) {
  res.send( { title: 'Express' });
});
router.post('/',async  function(req,res, next)  {
    console.log(req.body);

    let id = req.body.id;
    let method = req.body.method;
    let url = config.nodeUrl.pruning;
    let request = req.body;
    let headers = undefined;

    global.appLogger.info("routes, method: ", method, "id: ", id, "request: ", request, "header: ", headers);

    if(method === 'status'){
        let ret = await axios.request({ url: url, method: "POST", data: request, headers: headers });
        global.appLogger.info(ret.data);
        if(ret && ret.data){
            res.send(ret.data);
        }
    }else if(method === 'block'){
        let ret = await axios.request({ url: url, method: "POST", data: request, headers: headers });
        global.appLogger.info(ret.data);
        if(ret && ret.data){
            res.send(ret.data);
        }
    }else if(method === 'tx_search'){
        let ret = await axios.request({ url: url, method: "POST", data: request, headers: headers });
        global.appLogger.info(ret.data);
        if(ret && ret.data){
            res.send(ret.data);
        }
    }else{
        res.send({"Error": "method is error", 'method': method});
    }


})

module.exports = router;